﻿using Dapper;
using Entity;
using IRealize;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Data.SqlClient;
using Realize;
using System.Data;
using System.Threading.Tasks;

namespace WebApplication9.Controllers.ClientSet.RLSC
{
    public class DASCController : Controller
    {
        private readonly IConfiguration configuration;

        public DASCController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult SCCX()
        {
            return View();
        }

        public async Task<IActionResult> Search([FromBody] Human_File request)
        {

            ISCGL rLXLK = new SCGLD();



            // 调用服务层执行查询
            var result = await rLXLK.SHFSCXS(
               configuration["SqlServer"],
                request.first_kind_name,
                request.second_kind_name,
                request.third_kind_name,
                request.human_major_kind_name,
                request.hunma_major_name,
                request.regist_time,
                request.check_time,
                request.PageNumber,
                request.PageSize

            );


            // 返回分页结果
            return Ok(new
            {
                TotalCount = result.TotalCount,
                Items = result.Items,
                PageNumber = result.PageNumber,
                PageSize = result.PageSize
            });


        }

        public IActionResult LJSC()
        {
            return View();
        }


        public async Task<IActionResult> SCYM(int id)
        {
            return View();
        }

        public async Task<IActionResult> SCYM1(int id)
        {
            return View();
        }

        public async Task<IActionResult> GetHumanFile(int id)
        {
            try
            {
                // 从数据库查询档案信息
                RLXLK rLXLK = new RLXLK();
                Human_File hf = await rLXLK.SelectHFIDAsync(id, configuration["SqlServer"]);

                if (hf == null)
                {
                    return NotFound();
                }

                // 返回JSON格式数据
                return Ok(hf);
            }
            catch (Exception ex)
            {
                // 记录错误日志
                Console.WriteLine($"获取档案信息失败: {ex.Message}");
                return StatusCode(500, "服务器内部错误");
            }
        }


        public async Task<IActionResult> DeleteHumanFile([FromBody] Human_File human_File)
        {
            try
            {
                // 调用数据访问层执行逻辑删除
                ISCGL rLXLK = new SCGLD();
                int affectedRows = await rLXLK.UpdateStatus(human_File.huf_id, configuration["SqlServer"]);

                if (affectedRows > 0)
                {
                    return Ok(new { success = true, message = "删除成功" });
                }
                else
                {
                    return Ok(new { success = false, message = "删除失败，未找到该档案" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"逻辑删除失败: {ex.Message}");
                return StatusCode(500, new { success = false, message = "服务器错误，删除失败" });
            }

        }

        public IActionResult SCZS()
        {
            return View();
        }

        public IActionResult Index1()
        {
            return View();
        }

        public IActionResult Index3(int pageSize, int currentPage)
        {
            string sqlserver = configuration["SqlServer"];
            using (SqlConnection conn = new SqlConnection(sqlserver))
            {
                // 配置分页存储过程参数
                DynamicParameters param = new DynamicParameters();
                param.Add("@pageSize", pageSize);
                param.Add("@keyName", "huf_id");
                param.Add("@tableName", "human_file");
                param.Add("@currentPage", currentPage);
                param.Add("@checkStatus", 2);         // 添加审核状态条件
                param.Add("@fileStatus", 1);          // 添加文件状态条件
                param.Add("@totalRows", direction: ParameterDirection.Output, dbType: DbType.Int32);

                string sql = "exec [dbo].[proc_FenYeTj] @pageSize, @keyName, @tableName, @currentPage, @totalRows out, @checkStatus, @fileStatus";

                List<Human_File> list = conn.Query<Human_File>(sql, param).ToList();

                FenYe<Human_File> fenYecs = new FenYe<Human_File>();
                fenYecs.List = list;
                fenYecs.Rows = param.Get<int>("totalRows");

                return Json(fenYecs);
            }
        }

       
        //档案恢复查询页面
        public IActionResult DAHF()
        {
            return View();
        }

        public IActionResult DAFH1()
        {
            return View();
        }
        public IActionResult DAHF2(int id)
        {
            return View();
        }

        public IActionResult HFZS()
        {
            return View();
        }

        public async Task<IActionResult> Search1([FromBody] Human_File request)
        {

            ISCGL rLXLK = new SCGLD();



            // 调用服务层执行查询
            var result = await rLXLK.SHFSCXS1(
               configuration["SqlServer"],
                request.first_kind_name,
                request.second_kind_name,
                request.third_kind_name,
                request.human_major_kind_name,
                request.hunma_major_name,
                request.regist_time,
                request.check_time,
                request.PageNumber,
                request.PageSize

            );


            // 返回分页结果
            return Ok(new
            {
                TotalCount = result.TotalCount,
                Items = result.Items,
                PageNumber = result.PageNumber,
                PageSize = result.PageSize
            });


        }

        //档案恢复不输入条件空查页面
        public IActionResult DAHFKC()
        {
            return View();
        }

        //档案恢复不输入条件空查页面方法   
        public IActionResult DAFHKC1(int pageSize,int currentPage)
        {
            string sqlserver = configuration["SqlServer"];
            using (SqlConnection conn = new SqlConnection(sqlserver))
            {
                // 配置分页存储过程参数
                DynamicParameters param = new DynamicParameters();
                param.Add("@pageSize", pageSize);
                param.Add("@keyName", "huf_id");
                param.Add("@tableName", "human_file");
                param.Add("@currentPage", currentPage);
                param.Add("@checkStatus", 2);      // 动态传入审核状态
                param.Add("@fileStatus", 0);        // 动态传入文件状态
                param.Add("@totalRows", direction: ParameterDirection.Output, dbType: DbType.Int32);

                string sql = "exec [dbo].[proc_FenYeCXHF] @pageSize, @keyName, @tableName, @currentPage, @totalRows out, @checkStatus, @fileStatus";

                List<Human_File> list = conn.Query<Human_File>(sql, param).ToList();

                FenYe<Human_File> fenYecs = new FenYe<Human_File>();
                fenYecs.List = list;
                fenYecs.Rows = param.Get<int>("totalRows");

                return Json(fenYecs);
            }

        }

        public IActionResult DAFHCXXS(int id)
        {
            return View();
        }

        public async Task<IActionResult> HFUpdate([FromBody] Human_File human_File)
        {
            try
            {
                // 调用数据访问层执行逻辑删除
                ISCGL rLXLK = new SCGLD();
                int affectedRows = await rLXLK.UpdateStatusHF(human_File.huf_id, configuration["SqlServer"]);

                if (affectedRows > 0)
                {
                    return Ok(new { success = true, message = "恢复成功" });
                }
                else
                {
                    return Ok(new { success = false, message = "恢复失败，未找到该档案" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"逻辑删除失败: {ex.Message}");
                return StatusCode(500, new { success = false, message = "服务器错误，删除失败" });
            }

        }



        public async Task<IActionResult> QRDelete([FromBody] Human_File human_File)
        {


            ISCGL rLXLK = new SCGLD();

           // 执行删除操作（调用你的数据访问方法）
            int rowsAffected = await rLXLK.DeleteHF (human_File.huf_id, configuration["SqlServer"]);

            if (rowsAffected > 0)
            {
                // 删除成功，返回成功信息和受影响行数
                return Ok(new
                {
                    success = true,
                    message = "删除成功",
                    rowsAffected = rowsAffected
                });
            }
            else
            {
                // 未找到记录或删除失败
                return NotFound(new
                {
                    success = false,
                    message = "未找到要删除的档案记录"
                });
            }


        }

        public IActionResult DSSC()
        {

            return View();
        }


        //删除分页方法
        public IActionResult DeleteFY(int pageSize,int currentPage)
        {
            //string sqlserver = configuration["SqlServer"];
            //using (SqlConnection conn = new SqlConnection(sqlserver))
            //{
            //    // 配置分页存储过程参数
            //    DynamicParameters param = new DynamicParameters();
            //    param.Add("@pageSize", pageSize);
            //    param.Add("@keyName", "huf_id");
            //    param.Add("@tableName", "human_file");
            //    param.Add("@currentPage", currentPage);
            //    param.Add("@checkStatus", 2);      // 动态传入审核状态
            //    param.Add("@totalRows", direction: ParameterDirection.Output, dbType: DbType.Int32);

            //    string sql = "exec [dbo].[proc_FenYecheck_status] @pageSize, @keyName, @tableName, @currentPage, @totalRows out, @checkStatus";

            //    List<Human_File> list = conn.Query<Human_File>(sql, param).ToList();

            //    FenYe<Human_File> fenYecs = new FenYe<Human_File>();
            //    fenYecs.List = list;
            //    fenYecs.Rows = param.Get<int>("totalRows");

            //    return Json(fenYecs);
            //}

            string sqlserver = configuration["SqlServer"];
            using (SqlConnection conn = new SqlConnection(sqlserver))
            {
                //@pageSize, @tableName, @keyName, @currentPage, @rows
                //分页存储过程
                DynamicParameters param = new DynamicParameters();
                param.Add("@pageSize", pageSize);
                param.Add("@keyName", "huf_id");
                param.Add("@tableName", "human_file");
                param.Add("@currentPage", currentPage);
                param.Add("@totalRows", direction: ParameterDirection.Output, dbType: DbType.Int32);
                string sql = "exec [dbo].[proc_FenYedd] @pageSize, @keyName, @tableName, @currentPage, @totalRows out";
                List<Human_File> list = conn.Query<Human_File>(sql, param).ToList();

                FenYe<Human_File> fenYecs = new FenYe<Human_File>();
                fenYecs.List = list;
                fenYecs.Rows = param.Get<int>("totalRows");
                //return fenYecs;
                return Json(fenYecs);

            }

        }


    }
}
